package com.mma.assigment_registrationform;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginScreen extends Activity {
	private DataManager manager;

	private EditText email, city, password;
	private TextView loginUserName;
	private Button modify, search;
	private String username;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_screen);
		manager = DataManager.getInstance();

		Bundle bundle = getIntent().getExtras();
		username = bundle.getString("username");
		initLayout();
		if (manager.getUser(username).isAdmin()) {
			search.setVisibility(View.VISIBLE);
		} else {
			search.setVisibility(View.INVISIBLE);
		}

	}

	private void initLayout() {
		loginUserName = (TextView) findViewById(R.id.loginUserName);
		loginUserName.setText("Hello " + username);
		email = (EditText) findViewById(R.id.emailLoginScreen);
		city = (EditText) findViewById(R.id.cityLoginScreen);
		password = (EditText) findViewById(R.id.passwordLoginScreen);

		email.setText(manager.getUser(username).geteMail());
		city.setText(manager.getUser(username).getAddress());
		password.setText(manager.getUser(username).getPassword());

		modify = (Button) findViewById(R.id.modify);
		modify.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String passwordValue = password.getText().toString();
				String cityValue = city.getText().toString();
				String emailValue = email.getText().toString();

				if (passwordValue.length() != 0 && cityValue.length() != 0
						&& emailValue.length() != 0) {
					manager.modifyUser(username, passwordValue, cityValue,
							emailValue);

					finish();
				}

			}
		});

		search = (Button) findViewById(R.id.search);
		search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent searchUserIntent = new Intent(getApplicationContext(),
						SearchUser.class);
				startActivity(searchUserIntent);
			}
		});
	}
}
